#!/bin/bash
function setup_redis()
{
  local redis_version="3.2.11"
  echo_start "$1 ... ..."
  confirm_operate "install redis"
  if [ $? -eq 0 ]; then
    # 检查redis主机是否可被海云主机免密登录
    set_nopwdlogin_host $HRS_REDIS_HOST

    # 杀掉已经存在的进程
    echo_info "stop last process ... ..."
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "netstat -tnpl | grep -w $HRS_REDIS_PORT | grep redis > /dev/null"
    # 检查如果端口被占用，则检查redis文件的位置是否为海云设置的目录
    if [ $? -eq 0 ]; then
      ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "ls $HRS_REDIS_HOME/bin/redis-cli > /dev/null 2>&1"
      # redis文件的位置就是海云设置的目录
      if [ $? -eq 0 ]; then
        # 杀掉redis
        ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "$HRS_REDIS_HOME/bin/redis-cli -p $HRS_REDIS_PORT -h $HRS_REDIS_HOST shutdown && sleep 1"
        # 复查
        ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "netstat -tnpl | grep -w $HRS_REDIS_PORT | grep redis > /dev/null" && { die "redis port:$HRS_REDIS_PORT still exist, maybe [$HRS_REDIS_HOME/bin/redis-cli -p $HRS_REDIS_PORT -h $HRS_REDIS_HOST shutdown] fail! "; }
      else
        # 走到这里，说明端口被占用，但是redis不在海云指定的目录下。也就意味着该主机上有别的进程占用着这个端口
        die "Port [$HRS_REDIS_PORT] is occupied on [$HRS_REDIS_HOST]! please modify HRS_REDIS_PORT in install_env.conf."
      fi
    fi
    echo_done
    
    adduser_remote $HRS_REDIS_HOST

    # 清理环境：删掉上次安装的redis目录，重新上传安装包并解压
    echo_info "reset install env ... ..."
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "if [ -d $HRS_REDIS_HOME ]; then rm -rf $HRS_REDIS_HOME; fi" ||{ die "del home dir fail"; }
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "if [ -f $HYRENTMP_HOME/redis-${redis_version}.tar.gz ]; then  rm -rf $HYRENTMP_HOME/redis-${redis_version}.tar.gz; fi" ||{ die "del gz fail"; }
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "if [ -d $HYRENTMP_HOME/redis-${redis_version} ]; then  rm -rf $HYRENTMP_HOME/redis-${redis_version}; fi" ||{ die "del gz dir fail"; }
    scp $XDIR/packages/redis-${redis_version}.tar.gz $HRS_REDIS_HOST:$HYRENTMP_HOME >> $SETUP_LOGFILE || { die "scp $XDIR/packages/redis-${redis_version}.tar.gz to [$HRS_REDIS_HOST] fail."; }
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "tar zxf $HYRENTMP_HOME/redis-${redis_version}.tar.gz -C $HYRENTMP_HOME" ||{ die "unpack fail"; }
    echo_done

    # install gcc make
    echo_info "init compile env"
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "hash gcc > /dev/null 2>&1 || yum -y install gcc > /dev/null" || { die "setup gcc fail"; }
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "hash make > /dev/null 2>&1 || yum -y install make > /dev/null" || { die "setup make fail"; }
    echo_done

    # modify Makefile
    echo_info "compile ... ..."
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "sed -i \"s%^PREFIX.*%PREFIX=$HRS_REDIS_HOME%\" $HYRENTMP_HOME/redis-${redis_version}/src/Makefile" || { die "sed makefile fail! check $HYRENTMP_HOME/redis-${redis_version}/src/Makefile on $HRS_REDIS_HOST"; }
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "grep \"^PREFIX=$HRS_REDIS_HOME\" $HYRENTMP_HOME/redis-${redis_version}/src/Makefile > /dev/null" || { die "modify makefile fail! check $HYRENTMP_HOME/redis-${redis_version}/src/Makefile on $HRS_REDIS_HOST"; }
    echo_done
    # make
    echo_info "make ... ..."
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "cd $HYRENTMP_HOME/redis-${redis_version}/src && make -j 8 > /tmp/redis_make.log 2>/tmp/redis_make_err.log" || { die "redis_make fail! check log : /tmp/redis_make*.log on $HRS_REDIS_HOST"; }
    echo_done
    # make install 
    echo_info "install ... ..."
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "cd $HYRENTMP_HOME/redis-${redis_version}/src && make install > /tmp/redis_install.log 2>/tmp/redis_install_err.log" || { die "redis_install fail! check log : /tmp/redis_install*.log on $HRS_REDIS_HOST"; }
    # 复制redis.conf文件
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "cp $HYRENTMP_HOME/redis-${redis_version}/redis.conf $HRS_REDIS_HOME" || { die "cp redis.conf fail! check on $HRS_REDIS_HOST"; }
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "chown -R ${HYREN_USER}:${HYREN_USER} $HRS_REDIS_HOME" || { die "chown [$HRS_REDIS_HOME] fail on $HRS_REDIS_HOST"; }
    echo_done

    # edit conf file
    echo_info "setting conf file ... ..."
    # 修改bind
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "sed -i \"s%^[[:space:]]*bind .*%bind $HRS_REDIS_HOST%g\" $HRS_REDIS_HOME/redis.conf" || { die "bind_address fail"; }
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "grep -w \"^bind $HRS_REDIS_HOST\" $HRS_REDIS_HOME/redis.conf > /dev/null" || { die "seded bind_address fail"; }
    # port
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "sed -i \"s%^[[:space:]]*port 6379.*%port $HRS_REDIS_PORT%g\" $HRS_REDIS_HOME/redis.conf" || { die "redis_port fail"; }
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "grep -w \"^port $HRS_REDIS_PORT\" $HRS_REDIS_HOME/redis.conf > /dev/null" || { die "seded port fail"; }
    # pidfile
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "sed -i \"s%^[[:space:]]*pidfile .*%pidfile /var/run/redis_$HRS_REDIS_PORT.pid%g\" $HRS_REDIS_HOME/redis.conf" || { die "pidfile fail"; }
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "grep -w \"^pidfile /var/run/redis_$HRS_REDIS_PORT.pid\" $HRS_REDIS_HOME/redis.conf > /dev/null" || { die "seded pidfile fail"; }
    # logfile
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "sed -i \"s%^[[:space:]]*logfile .*%logfile $HRS_REDIS_LOGS%g\" $HRS_REDIS_HOME/redis.conf" || { die "logfile fail"; }
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "grep -w \"^logfile $HRS_REDIS_LOGS\" $HRS_REDIS_HOME/redis.conf > /dev/null" || { die "seded logfile fail"; }
    # dir
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "sed -i \"s%^[[:space:]]*dir .*%dir $HRS_REDIS_HOME%g\" $HRS_REDIS_HOME/redis.conf" || { die "logfile fail"; }
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "grep -w \"^dir $HRS_REDIS_HOME\" $HRS_REDIS_HOME/redis.conf > /dev/null" || { die "seded dir fail"; }
    # daemon
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "sed -i \"s%[[:space:]]*daemonize no%daemonize yes%\" $HRS_REDIS_HOME/redis.conf" || { die "set daemonize fail"; }
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "grep -w \"^daemonize yes\" $HRS_REDIS_HOME/redis.conf > /dev/null" || { die "seded daemon fail"; }
    echo_done

    # start redis
    echo_info "start redis ... ..."
    ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "$HRS_REDIS_HOME/bin/redis-server $HRS_REDIS_HOME/redis.conf" || { die "start redis_server fail, check logfile : $HRS_REDIS_LOGS"; }
    echo_done

    echo_info "set profile on all hyren hosts ... ..."
    # 给所有主机，设置redis的环境变量
    local value_arr=($HYREN_ALLHOSTS)
    for i in ${value_arr[@]}; do
      #ssh -o StrictHostKeyChecking=no $i 'bash -s' < $XDIR/util/func_rmot_setprofile.sh "HRS\ ZOOKEEPER\ ENV" "HRSCOMMENT" "$HYREN_USER"
      echo_doing "  host : $i >>>>>> "
      ssh -o StrictHostKeyChecking=no $i 'bash -s' < $XDIR/util/func_rmot_setprofile.sh "HRS_REDIS_HOST" "$HRS_REDIS_HOST" "$HYREN_USER"
      ssh -o StrictHostKeyChecking=no $i 'bash -s' < $XDIR/util/func_rmot_setprofile.sh "HRS_REDIS_PORT" "$HRS_REDIS_PORT" "$HYREN_USER"
      ssh -o StrictHostKeyChecking=no $i 'bash -s' < $XDIR/util/func_rmot_setprofile.sh "HRS_REDIS_HOME" "$HRS_REDIS_HOME" "$HYREN_USER"
      ssh -o StrictHostKeyChecking=no $i 'bash -s' < $XDIR/util/func_rmot_setprofile.sh "HRS_REDIS_LOGS" "$HRS_REDIS_LOGS" "$HYREN_USER"
      echo_done
    done
    echo_done

    echo_success "$1"
    echo_info "****************************************"
    echo_info "RDSRUN HOST    dir  : $HRS_REDIS_HOST"
    echo_info "RDSRUN HOME    dir  : $HRS_REDIS_HOME"
    echo_info "RDSRUN LOGS   file  : $HRS_REDIS_LOGS"
    echo_info "RDSRUN SERVER PORT  : $HRS_REDIS_PORT"
    echo_info "****************************************"
    
    echo_passed "$1 done."
  else
    echo_passed "$1 SKIP"
  fi
}
